import { ecs } from "../../core/ecs/ECS";
import { hs } from "../../HallServer";
import { Config } from "../../module/config/Config";
import { ShareConfig } from "../../module/config/ShareConfig";
import { Room } from "../../module/room/Room";
import { rs } from "../../RoomServer";
import { RoomModel } from "./model/RoomModel";
import { RoomServerStartComp, RoomServerStartSystem } from "./sys/RoomServerStart";

@ecs.register(`RoomServer`)
export class RoomServer extends ecs.Entity {
    RoomModel:RoomModel = null!;

    protected init() {
       this.addComponents<ecs.Comp>(RoomModel);
    }

    /** 开启网关服务器 */
    start() {
        this.add(RoomServerStartComp);
    }

    createRoom(name:string):Room {
        let addr = `${ShareConfig.https ? "https" : "http"}://127.0.0.1:${Config.room.port}/`;
        const entity = ecs.getEntity(Room);
        rs.RoomModel.rooms.set(entity.RoomModel.roomId, entity);
        entity.RoomModel.roomName = name;
        entity.RoomModel.roomServerAddr = addr;
        return entity;
    }
}

export class EcsRoomSystem extends ecs.System {
    constructor() {
        super();

        this.add(new RoomServerStartSystem());
    }
}